Module : Module manager

更新时间:
2024-03-13
下载文档

Module : Module manager

JSRE supports two module types: ECMAScript 6 and Node.js module management. The ECMAScript 6 type module only supports the *.js module. For the ECMAScript 6 module, please refer to the JavaScript related documentation.

Importing a module in JSRE uses the require() function, similar to Node.js, and supports *.js, *.json and *.so modules, where *.so is a native module, which can be written in C/C++. JSRE recommends that some computationally intensive modules be written in native language for higher efficiency.

Example

var fs = require('fs');
var Tcp = require('tcp');

Path Rules

When require() a module, JSRE first determines the module path. If the path start with ./ or /, JSRE will search using the directory specified by the user. For example: ./foo JSRE will search for the current directory (current file path) ./aaa.* and attempts to load this module. If the starting character is not ./ or / JSRE will search for JSRE installation environment path and import system built-in module.

Priority Rules

JSRE supports *.js, *.json and *.so modules. The JSRE module search order is as follows 1: *.so, 2: *.js, 3: *.json, for example require('./foo'), JSRE first searches for ./foo.so shared library, if not found then search for ./foo.js If still not found, then search for ./foo.json If none is found, the modules load fails.

Mixin Module

JSRE supports mixin modules, which are native and JavaScript mixin. If we want import foo module using require(foo). The JSRE loader first lookup for foo.so. If found, it start native module import and then continues to lookup for foo.js. If it exists, the module loaded use foo.so's exported as a native object for foo.js, and then parse foo.js file. In the foo.js code, the native object is a global variable which is foo.so exported, and then the foo.js export is used as the whole module export.

Module Object

Each *.js file contains a module global object that contains some information about the current file module, such as the module name and etc.

module

  • id {String} This module ID.
  • tag {String} This module debug prefix.
  • parent {Object} Parent module.
  • directory {String} This module directory.
  • filename {String} This module filename, EdgerOS 1.5.0 and later versions support.
  • exports {Object} Modules exports.

exports is the most commonly used, this decides what the module exports.

Example

function add(a, b) {
  return a + b;
}

function dec(a, b) {
  return a - b;
}

module.exports.add = add;
module.exports.dec = dec;

We can easily use this module:

Example

var foo = require('./foo');

var a = foo.add(1, 2);
var b = foo.dec(2, 1);

console.log(a, b);

We can export a class:

Example

class Class {
  constructor() {
    this.a = 1;
  }
}

module.exports.Class = Class;

We can easily use this module:

Example

var foo = require('./foo');

var obj = new foo.Class();

console.log(obj.a);

We also can export main class:

Example

var bbb = 1;

class Class {
  constructor() {
    this.a = 1;
  }
}

module.exports = Class;

// Also can add some variables
module.exports.aaa = 1;
module.exports.bbb = bbb;

// Also can add some functions
function f1() {
  console.log('f1() call');
}

module.exports.func = f1;

We can easily use this module:

Example

var Foo = require('./foo');

var obj = new Foo();

Foo.func();

console.log(obj.a);
console.log(Foo.aaa);
console.log(Foo.bbb);

Debug Print

Module may need some debugging print, The following methods can be used:

Example

class Class {
  constructor() {
    this.a = 1;
    console.tag(module, 'new object construct');
  }
}

module.exports = Class;
var Foo = require('./foo');

console.tagEnable = true; // Enable console.tag()

var obj = new Foo();
文档内容是否对您有所帮助?
有帮助
没帮助